extends Control

@export var text_handler: TextHandler
@export var infos: Control

@onready var color_picker = %ColorPicker
@onready var text_input = %TextInput
@onready var speed_slider = %SpeedSlider
@onready var font_size_slider = %FontSizeSlider
@onready var spacing_slider = %SpacingSlider
@onready var text_color_picker = %TextColorPicker

var prev_text: Array

func _ready() -> void:
	var popup: PopupMenu = %PrevTextMenu.get_popup()
	popup.id_pressed.connect(on_prev_select)
	
	prev_text = ConfigManager.config.get_value(TextHandler.SECTION_NAME, "prev_text", ["Neon Light"])
	
	color_picker.color = ConfigManager.config.get_value(ConfigManager.SECTION_BACKGROND, "color", Color.hex(0xa042e3ff))
	text_color_picker.color = text_handler.text_color
	text_input.text = text_handler.text
	font_size_slider.value = text_handler.font_size_percent
	speed_slider.value = text_handler.roll_speed
	spacing_slider.value = text_handler.spacing
	
	
	var picker = text_color_picker.get_picker() as ColorPicker
	picker.picker_shape = color_picker.picker_shape
	picker.sampler_visible = color_picker.sampler_visible
	picker.color_modes_visible = color_picker.color_modes_visible
	picker.sliders_visible = color_picker.sliders_visible
	picker.hex_visible = color_picker.hex_visible
	picker.presets_visible = color_picker.presets_visible


func _notification(what: int) -> void:
	if what == NOTIFICATION_VISIBILITY_CHANGED:
		if visible:
			var menu = %PrevTextMenu
			var popup: PopupMenu = menu.get_popup()
			popup.clear()
			
			for text in prev_text:
				popup.add_item(text)


func _gui_input(event: InputEvent) -> void:
	if event is InputEventMouseButton:
		if event.pressed:
			self.visible = false


func on_confirm() -> void:
	var bg_color = color_picker.color
	var roll_speed = speed_slider.value
	var text_idx := prev_text.find(text_input.text)
	prev_text.push_front(prev_text.pop_at(text_idx) if text_idx != -1 else text_input.text)
	prev_text.resize(mini(5, prev_text.size()))
	var text_ratio = font_size_slider.value
	var spacing = spacing_slider.value
	var text_color = text_color_picker.color
	
	ConfigManager.config.set_value(ConfigManager.SECTION_BACKGROND, "color", bg_color)
	ConfigManager.config.set_value(TextHandler.SECTION_NAME, "roll_speed", roll_speed)
	ConfigManager.config.set_value(TextHandler.SECTION_NAME, "size_ratio", text_ratio)
	ConfigManager.config.set_value(TextHandler.SECTION_NAME, "spacing_ratio", spacing)
	ConfigManager.config.set_value(TextHandler.SECTION_NAME, "prev_text", prev_text)
	ConfigManager.config.set_value(TextHandler.SECTION_NAME, "color", text_color)
	
	RenderingServer.set_default_clear_color(bg_color)
	text_handler.set_roll_speed(roll_speed)
	text_handler.set_text(prev_text[0])
	text_handler.set_text_percent(text_ratio)
	text_handler.set_spacing(spacing)
	text_handler.set_text_color(text_color)
	self.visible = false


func on_info_click() -> void:
	infos.visible = true


func on_prev_select(idx: int) -> void:
	var popup: PopupMenu = %PrevTextMenu.get_popup()
	text_input.text = popup.get_item_text(idx)
